// import store from '../vuex/store.js'

export const telReg = /^\s*1\d{10}$/

export const phoneReg = /^(0[0-9]{2,3}\-)([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/

export const cardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/

export const mailReg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/

export const toastShow = (title:string, type?:any, callback?:any) => {
	uni.showToast({
		title: title,
		icon: type || 'none',
		success: callback
	})
}

export const toWxLogin = () => {
	let callBackUrl = encodeURIComponent(window.location.origin)
	let search = window.location.search
	// let pathname = window.location.pathname
	// console.log(search, '111')
	// console.log(pathname, '222')
	// console.log(window.location)
	
	return	document.location.href =
		`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb2488557c7a8e1c1&redirect_uri=${callBackUrl}&response_type=code&scope=snsapi_userinfo&state=${search}#wechat_redirect`
}

export const getUrlParam = (name: string) => {
	let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	let r = window.location.search.substr(1).match(reg);  //匹配目标参数
	if (r != null) return unescape(r[2]);
	return null; //返回参数值
}

//是否为空
export const objIsEmpty = (obj: any ) => {
	for(var i in obj){
	    return false; //不为空
	}
	return true;  //为空
}

//四舍五入处理
export const toFixenFn = ( num: string, n: number = 2 ) => {
	if(typeof parseFloat(num) != 'number') {
		return num
	}
	  
	num = num + ''
	if(num.split('.')[1] && num.split('.')[1].length > 2) {
		let a:any = num.split('.')[1]
		if(a[a.length - 1] == 5) {
			a = a + '1'
			return parseFloat(`${num.split('.')[0]}.${a}`).toFixed(n)
		}else {
			return parseFloat(num).toFixed(n)
		}
	  }else {
		return parseFloat(num).toFixed(n)
	}
}

//去掉前后空格
export const trim = (strs: string) => {
	if (Object.prototype.toString.call(strs) === "[object String]") {
		return strs.replace(/(^\s*)|(\s*$)/g, '');
	}
}

export const getPlatform = () => {
	let platForm = undefined
	// #ifdef APP-PLUS
	platForm = "APP"
	// #endif
	// #ifdef APP-PLUS-NVUE
	platForm = "APP"
	// #endif
	// #ifdef H5
	platForm = "H5"
	// #endif
	// #ifdef MP-WEIXIN
	platForm = "MP-WEIXIN"
	// #endif
	
	return platForm
}

//是否在微信公众号内
export const isWxemsCnpl = () => {
	let ua = navigator.userAgent.toLowerCase() as any;
	if(ua.match(/MicroMessenger/i) == "micromessenger") {
		return true
	}else {
		return false
	}
}

// 获取图片
export const getImgUrl = async (tempFilePaths?: any, cb?: any, namekey?: string) => {
	let res = await uni.$api.qiNiuToken()
	if(res) {
		let obj= res.data
		tempFilePaths.map((item:string) => {
			uni.uploadFile({
			    url: 'https://upload-z2.qiniup.com', //华南区服务地址
			    name: 'file',
			    filePath: item,
			    header: {
			        ...(getPlatform() == 'MP-WEIXIN'? {
			    		"Content-Type": "multipart/form-data"
			    	}: {})
			    },
			    formData: {
			        token: obj.token,
					...(namekey? {
						key: `${obj.key}${namekey}`
					}: {})
			    },
			    success: function (res) {
			        let data = JSON.parse(res.data)
					let url = obj.domain + data.key
					typeof cb == 'function' && cb(url)
			    },
			    fail: function (res) {
					console.log(res)
					uni.hideLoading()
			        toastShow('上传失败')
			    }
			})
		})
	}else {
		uni.hideLoading()
	}
}

